package framework.bean;

import framework.ioc.container.ObjectPool;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 马洁
 * @date 2020/3/10 - 21:49
 */
public class Action {
    private Class<?> cls;
    private Method method;
    private Class<?>[] args;

    public Action(Class<?> cls, Method method, Class<?>[] args) {
        this.cls = cls;
        this.method = method;
        this.args = args;
    }

    public Action(Class<?> cls, Method method) {
        this.cls = cls;
        this.method = method;
    }

    public Action(Class<?> cls) {
        this.cls = cls;
    }

    public boolean exeMethod(ObjectPool objectPool) {
        try {
            if (args.length == 0) {
                this.method.invoke(objectPool.getClassInstance(cls));
            } else {
                this.method.invoke(objectPool.getClassInstance(cls), (Object) args);
            }

            return true;
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void judgeAndSetArgs() {
        if (method.getParameterCount() >= 0) {
            this.args = method.getParameterTypes();
        }
    }

    public boolean clear() {
        this.cls = null;
        this.method = null;
        this.args = null;

        return true;
    }

    @Override
    public String toString() {
        return "Action{" +
                "cls=" + cls +
                ", method=" + method +
                '}';
    }

    class Cls{
        private Class<?> cls;
    }
}
